package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.ParseException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Subject;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import model.Lab;

/**
 *
 * @author cska
 *
 */

public class LabDlg extends JDialog implements ocDialog {

    protected GridBagConstraints c;
    protected JComboBox subjectBox;
    protected Lab lab;
    public boolean fl;
    protected JLabel l1, l2, l3, l4;
    protected JFormattedTextField begin;
    protected JFormattedTextField end;
    protected JTextField text;
    protected JPanel bp;
    protected JButton ok;
    protected JButton cancel;

    public LabDlg(JFrame owner, boolean modal, String title, Lab lab, Subject subject) {
        super(owner, title, modal);

        if (lab != null)
            this.lab = lab;
        else
            this.lab = new Lab(getDate(), getDate(), subject, "");

        setLayout(new GridBagLayout());
        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        l2 = new JLabel("Дата выдачи:", JLabel.RIGHT);
        add(l2,c);
        c.gridy = 1;
        l3 = new JLabel("Дата сдачи:", JLabel.RIGHT);
        add(l3,c);
        c.gridy = 2;
        l4 = new JLabel("Задание:", JLabel.RIGHT);
        add(l4,c);

        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 1.0;
        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("####.##.##");
        } catch (ParseException ex) {
            Logger.getLogger(TeacherWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        begin = new JFormattedTextField(mask);
        begin.setText(this.lab.getBegin_date());
        begin.setHorizontalAlignment(JFormattedTextField.CENTER);
        add(begin,c);
        c.gridy = 1;
        try {
            mask = new MaskFormatter("####.##.##");
        } catch (ParseException ex) {
            Logger.getLogger(TeacherWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
        end = new JFormattedTextField(mask);
        end.setText(this.lab.getEnd_date());
        end.setHorizontalAlignment(JFormattedTextField.CENTER);
        add(end,c);
        c.gridy = 2;
        text = new JTextField(this.lab.getText());
        text.setHorizontalAlignment(JTextField.CENTER);
        add(text,c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        c.weightx = 1.0;
        c.weighty = 1.0;
        bp = new JPanel(new GridBagLayout());
        add(bp,c);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        bp.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        bp.add(cancel,c);

        setSize(320,180);
        setResizable(false);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-320)/2, (screen.height-180)/2);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);
    }

    public void ok() {
        fl = true;
        lab.setBegin_date(begin.getText());
        lab.setEnd_date(end.getText());
        lab.setText(text.getText());
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    public Lab getLab() {
        return lab;
    }

    public String getDate() {
        Calendar cal = Calendar.getInstance();
        cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = (1 + cal.get(Calendar.MONTH));
        int year = cal.get(Calendar.YEAR);
        String date_str = year + "." + (month < 10 ? "0" + month : month ) + "." + (day < 10 ? "0" + day : day);
        return date_str;
    }

}
